﻿Imports lbGlobalcom
Public Class fAdminMenus
    Private mAdmiMenu As cMenu
    Private Sub CargaUseroGroup()
        Try
            If rbUsuarios.Checked Then
                lbUserOGroup.Text = "Usuarios"
                cbUserOGroup.Items.Clear()
                For Each dr As DataRow In cUsuariosOracle.GetUsuarios.Rows
                    cbUserOGroup.Items.Add(dr("USERNAME"))
                Next
            ElseIf rbGrupos.Checked Then
                lbUserOGroup.Text = "Roles"
                cbUserOGroup.Items.Clear()
                For Each dr As DataRow In cUsuariosOracle.GetRoles.Rows
                    cbUserOGroup.Items.Add(dr("ROLE"))
                Next
            End If
            cbUserOGroup.Text = ""
            CargaMenu()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub CargaTipoObjeto()
        Try
            If cbTipoObjeto.SelectedValue IsNot Nothing Then _
                GlO_OBJETOSTableAdapter.FillByTipoObjeto(DsMenus.GLO_OBJETOS, cbTipoObjeto.SelectedValue)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub fAdminMenus_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            GlO_DATOSTABLASTableAdapter.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "ob")
            CargaUseroGroup()
            CargaTipoObjeto()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub cbTipoObjeto_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTipoObjeto.SelectedIndexChanged
        CargaTipoObjeto()
    End Sub

    Private Sub rb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGrupos.CheckedChanged, rbUsuarios.CheckedChanged
        CargaUseroGroup()
    End Sub

    Private Sub btnAddNodo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNodo.Click
        If txtDescripcionNodo.Text.Trim.Length > 0 Then
            Dim mNodo As New TreeNode
            mNodo.Text = txtDescripcionNodo.Text
            mNodo.ImageIndex = 1
            mNodo.SelectedImageIndex = 1
            mNodo.StateImageIndex = 1
            If tvMenuByUser.SelectedNode IsNot Nothing Then
                tvMenuByUser.SelectedNode.Nodes.Add(mNodo)
            Else
                tvMenuByUser.Nodes.Add(mNodo)
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnAddObject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddObject.Click
        If lstObjetos.SelectedValue IsNot Nothing Then
            If tvMenuByUser.SelectedNode IsNot Nothing Then
                Dim mNodo As New TreeNode
                mNodo.Text = lstObjetos.Text
                mNodo.Tag = lstObjetos.SelectedValue
                mNodo.ImageIndex = cbTipoObjeto.SelectedIndex + 2
                mNodo.SelectedImageIndex = cbTipoObjeto.SelectedIndex + 2
                mNodo.StateImageIndex = cbTipoObjeto.SelectedIndex + 2
                tvMenuByUser.SelectedNode.Nodes.Add(mNodo)
            End If
        End If
    End Sub
    Private Sub CargaMenu()
        Try
            Me.Cursor = Cursors.WaitCursor
            mAdmiMenu = New cMenu(cbUserOGroup.Text, tvMenuByUser, ImgList)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    Private Sub cbUserOGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbUserOGroup.SelectedIndexChanged
        CargaMenu()
    End Sub

    Private Sub btnSalvaMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalvaMenu.Click
        Try
            If mAdmiMenu IsNot Nothing Then
                Me.Cursor = Cursors.WaitCursor
                mAdmiMenu.SalvaTV()
                MessageBox.Show("Se salvo menu con exito", "Salva Menu", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub EliminaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminaToolStripMenuItem.Click
        If MessageBox.Show("Esta seguro que desea eliminar este nodo y sus hijos", "Elimina Nodo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            If tvMenuByUser.SelectedNode IsNot Nothing Then
                tvMenuByUser.SelectedNode.Remove()
            End If
        End If
    End Sub
End Class